package pl.pr3sto

import grails.test.*
import pl.pr3sto.auctions.Category
import pl.pr3sto.security.Person
import pl.pr3sto.security.Profile
import pl.pr3sto.security.Role
import pl.pr3sto.test.mocking.MockLogger

class BootStrapServiceTests extends GrailsUnitTestCase {
    
	BootStrapService service
	
	protected void setUp() {
        super.setUp()
		String.metaClass.encodeAsMD5 = {
			org.codehaus.groovy.grails.plugins.codecs.MD5Codec.encode(delegate)
		}
		BootStrapService.metaClass.getLog = {
			return MockLogger.INSTANCE
		}
		service = new BootStrapService()
    }

    protected void tearDown() {
        super.tearDown()
    }

	void testCreateInitialCommonData() {
		mockDomain(Category)
		mockProfileBuilder()
		
		// when
		service.createInitialCommonData()
		
		// then
		assert Profile.countByRole(Role.GUEST) == 1
		assert Profile.countByRole(Role.ADMIN) == 1
		assert Profile.countByRole(Role.SUPERADMIN) == 1
		// TODO: add new asserts when the method changes, so that it checks everything that service initializes
	}
	
	void testCreateDevTestData() {
		mockDomain(Profile)
		mockDomain(Person)
		
		// when
		service.createDevTestData()
		
		// then
		assert Profile.countByRole(Role.USER) == 3
		// TODO: add new asserts when the method changes, so that it checks everything that service initializes
	}
	
	void testCreateAdmins() {
		mockDomain(Profile)
		mockDomain(Person)
		
		// when
		service.createAdmins()
		
		// then
		assert Profile.countByRole(Role.ADMIN) == 1
		assert Profile.countByRole(Role.SUPERADMIN) == 1
		assert Profile.findByLogin('admin').person.lastName == 'Admin'
		assert Profile.findByLogin('god').person.lastName == 'Superadmin'
	}
	
	void testCreateGuestUser() {
		mockProfileBuilder()
		
		// given
		assert Profile.countByRole(Role.GUEST) == 0
		
		// when
		service.createGuestUser()
		
		// then
		assert Profile.countByRole(Role.GUEST) == 1
	}
	
	void testCreateUsers() {
		mockDomain(Profile)
		mockDomain(Person)
		
		// when
		service.createUsers()
		
		// then
		assert Profile.count() == 3
		assert Person.count() == 3
		assert Profile.findByLogin('user1').person.city == 'Katowice'
		assert Profile.findByLogin('user2').person.phone == '1524356'
		assert Profile.findByLogin('user3').person.firstName == 'Karol'
	}
	
	void testCreateCategories() {
		mockDomain(Category)
		
		// when
		service.createCategories()
		
		// then
		assert Category.count() > 0
		
	}
	
	void testCreateCategoriesBranch() {
		mockDomain(Category)
		
		// given
		Map categories = [
			"category.antiquities": [
				"category.paintings": null,
				"category.furniture": null,
				"category.sculpture": [
					"category.wooden": null,
					"category.stone": null,
					"category.metal": null
				]
			]
		]
		
		// when
		service.createBranchOfCategories(null, categories)
		
		// then
		assert Category.count() == 7
		assert Category.findByName("category.antiquities").parentCategory == null
		assert Category.findByName("category.paintings").parentCategory.name == "category.antiquities"
		assert Category.findByName("category.furniture").parentCategory.name == "category.antiquities"
		assert Category.findByName("category.sculpture").parentCategory.name == "category.antiquities"
		assert Category.findByName("category.wooden").parentCategory.name == "category.sculpture"
		assert Category.findByName("category.stone").parentCategory.name == "category.sculpture"
		assert Category.findByName("category.metal").parentCategory.name == "category.sculpture"
	}
	
	private void mockProfileBuilder() {
		mockDomain(Profile)
		mockDomain(Person)
		Person.metaClass.'static'.build = { map ->
			new Person(firstName: 'guest', lastName: 'guest', email:' emeail@aa.pl', phone:'32432', address: 'fdssfdgb', city:'sdfdfs', postCode:'23432' ).save()
		}
		Profile.metaClass.'static'.build = { map ->
			new Profile(login: 'guest', password: 'guest', role: Role.GUEST, person: Person.list()[0]).save()
		}
	}
}
